package com.app.basic.controller;


import com.app.basic.dao.entity.SysUser;
import com.app.basic.server.UserServiceFace;
import com.app.model.dto.LoginDTO;
import com.app.model.dto.RegisterDTO;
import com.app.utils.CaptchaGenerator;
import com.app.utils.Result;
import com.app.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


@RequestMapping("/system")
@RestController
@Api(tags = "用户操作")
public class SysUserFaceController {

    @Resource
    private UserServiceFace userServiceFace;

    /**
     * 自定义token登录
     *
     * @return
     */
    @ApiOperation("登录")
    @PostMapping(value = "/login")
    public Result<Object> login(@RequestBody LoginDTO loginDTO) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, IOException, BadPaddingException, InvalidKeyException {
        return Result.ok().setMsg("登录成功").setShowMsg(true).setData(userServiceFace.login(loginDTO));
    }


    @ApiOperation("验证码")
    @PostMapping(value = "/captcha")
    public Result<Object> captcha() throws IOException {
        return Result.ok().setData(CaptchaGenerator.generate());
    }


//    @ApiOperation("获取角色列表")
//    @PostMapping("/roles")
//    public Result roles() {
//        return Result.ok().setData(userServiceFace.roles());
//    }


    @ApiOperation("获取当前用户")
    @GetMapping("currentUser")
    public Result<SysUser> currentUser() {
        return Result.ok(UserContext.getUser());
    }


    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public Result logout() {
        SecurityUtils.getSubject().logout();
        return Result.ok().setMsg("成功退出").setShowMsg(true);
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterDTO registerDTO) {
        return Result.ok().setMsg("注册成功").setShowMsg(true).setData(userServiceFace.register(registerDTO));
    }


}
